package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("api/hosp")
public class ApiController {
    @Autowired
    private HospitalSetService hospitalSetService; // 医院设置业务实现类
    @Autowired
    private HospitalService hospitalService;  // 医院接口实现类
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("删除科室信息")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        // 获取请求参数   // 调用工具类进行转换
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2 根据传递过来医院编码，查询数据库，查询签名   // 参数校验.....一大堆....
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(depcode) ) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        departmentService.removeDepartmentByHashcodeAndDecade(hoscode,depcode);

        return Result.ok();
    }



    @ApiOperation("查询排班分页信息")
    @PostMapping("schedule/list")
    public Result findScheduleList(HttpServletRequest request) {
        // 获取请求参数   // 调用工具类进行转换
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //2 根据传递过来医院编码，查询数据库，查询签名
        // 参数校验.....一大堆....
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        // 封装分页信息  // 当前页 和 每页记录数
        String pageNumStr = (String) paramMap.get("pageNum");
        String pageSizeStr = (String) paramMap.get("pageSize");
        int pageNum = StringUtils.isEmpty(pageNumStr) ? 1 : Integer.parseInt(pageNumStr);
        int pageSize = StringUtils.isEmpty(pageSizeStr) ? 5 : Integer.parseInt(pageSizeStr);
        //TODO 签名校验...
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        // 调用业务层查询分页数据
        Page<Schedule> pageModel = scheduleService.findSchedulePage(pageNum, pageSize, scheduleQueryVo);
        // 返回数据结果
        return Result.ok(pageModel);
    }


    @ApiOperation("上传排班信息")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 获取请求参数   // 调用工具类进行转换
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(hosScheduleId)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验 ....// 调用保存方法
        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }

    @ApiOperation("删除排班信息")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        // 获取请求参数   // 调用工具类进行转换
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(hosScheduleId)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验 ....// 调用方法
        scheduleService.removeSchedule(hoscode,hosScheduleId);
        return Result.ok();
    }


    @ApiOperation("上传科室信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取请求参数   // 调用工具类进行转换
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKeyByHosCode(hoscode))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用保存方法
        departmentService.saveDepartment(paramMap);
        return Result.ok();
    }

    @ApiOperation("查询科室分页信息")
    @PostMapping("department/list")
    public Result findDepartmentList(HttpServletRequest request) {
        // 获取请求参数   // 调用工具类进行转换
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2 根据传递过来医院编码，查询数据库，查询签名
        // 参数校验.....一大堆....
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        // 封装分页信息
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //TODO 签名校验

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pageModel = departmentService.findDepartmentPage(page, limit, departmentQueryVo);

        // 返回数据结果
        return Result.ok(pageModel);
    }


    @ApiOperation("上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 获取请求参数   // 调用工具类进行转换
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        hospitalService.saveHospital(paramMap);

        return Result.ok();
    }


    @ApiOperation("查询医院接口")
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request) {
        // 获取请求参数   // 调用工具类进行转换
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        // 参数校验.....一大堆....
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        // 返回数据结果
        return Result.ok(hospital);
    }


}
